import 'package:flutter/material.dart';

void main() => runApp(TextApp());

///应用的根布局
class TextApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    ///构建Materia Desin 风格的应用程序
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        brightness: Brightness.light,

        ///主题是蓝色
        colorSchemeSeed: Colors.blue,
        //启用
        useMaterial3: true,
      ),

      ///默认的首页面
      home: NavaHomePage2(),
    );
  }
}

class NavaHomePage2 extends StatefulWidget {
  const NavaHomePage2({super.key});

  @override
  _NavaHomePage2State createState() => _NavaHomePage2State();
}

class _NavaHomePage2State extends State<NavaHomePage2> {
  int _selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: buidPage(),
      bottomNavigationBar: BottomNavigationBar(
        //fixed 自适应，shifting 选择放大
        type: BottomNavigationBarType.fixed,
        //这是一个 BottomNavigationBarItem 数组 而且至少2个
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
          BottomNavigationBarItem(icon: Icon(Icons.business), label: '分类'),
          BottomNavigationBarItem(icon: Icon(Icons.school), label: '发现'),
          BottomNavigationBarItem(icon: Icon(Icons.school), label: '我的'),
        ],
        //当前选中
        currentIndex: _selectedIndex,
        //当前选中
        selectedItemColor: Colors.teal,
        //导航栏的点击事件，通过它来切换页面
        onTap: _onItemTapped,
        //底部导航栏颜色
        backgroundColor: Color(0xfffafafa),
      ),
    );
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  buidPage() {
    if (_selectedIndex == 0) {
      return Text("页面一");
    }

    if (_selectedIndex == 2) {
      return Text("页面二");
    }

    if (_selectedIndex == 3) {
      return Text("页面三");
    }

    return Text("页面四");
  }
}
